package com.ccf.business.etl.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.ccf.business.etl.model.Task;
import com.ccf.business.etl.service.ITaskService;
import com.common.model.pojo.Query;
import com.common.model.result.PageResult;
import com.common.model.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;

/**
 * 任务调度表
 *
 * @author zhaoj
 * @date 2021-12-01 15:15:53
 */
@Slf4j
@RestController
@RequestMapping("/task")
@Api(tags = "任务调度表")
public class TaskController {
    @Autowired
    private ITaskService taskService;

    /**
     * 列表
     */
    @ApiOperation(value = "分页查询列表")
    @GetMapping
    public PageResult list(Query query) {
        return taskService.findList(query);
    }


    /**
     * 查询
     */
    @ApiOperation(value = "查询")
    @GetMapping("/{id}")
    public Result getById(@PathVariable String id) {
        Task model = taskService.getById(id);
        return Result.succeed(model, "查询成功");
    }

    /**
     * 新增or更新
     */
    @ApiOperation(value = "保存")
    @PostMapping
    public Result save(@RequestBody Task task) {
        if (taskService.isExistField(task.getId(), task.getName(),
                "name")){
            return Result.failed("任务名称已经存在");
        }
        boolean submit = false;
        try {
            submit = taskService.submit(task);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.failed(e.getMessage());
        }

        return Result.status(submit, "");
    }

    /**
     * 删除
     */
    @ApiOperation(value = "删除")
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable String id) {
        //批量删除
        boolean result = taskService.deleteBatch(Arrays.asList(StrUtil.split(id, Constants.COMMA)));
        return Result.status(result, "");
    }

    /**
     * 启动调度任务
     */
    @ApiOperation(value = "启动调度任务")
    @PostMapping("/start")
    public Result start(@RequestBody Task task) {
        if(StrUtil.isEmpty(task.getId())) {
            return Result.failed("ID不能为空");
        }
        boolean result = taskService.start(Arrays.asList(StrUtil.split(task.getId(), Constants.COMMA)));
        return Result.status(result, "");
    }

    /**
     * 停止调度任务
     */
    @ApiOperation(value = "停止调度任务")
    @PostMapping("/stop")
    public Result stop(@RequestBody Task task) {
        if(StrUtil.isEmpty(task.getId())) {
            return Result.failed("ID不能为空");
        }
        boolean result = taskService.stop(Arrays.asList(StrUtil.split(task.getId(), Constants.COMMA)));
        return Result.status(result, "");
    }

    /**
     * 手动执行一次
     */
    @ApiOperation(value = "手动执行一次")
    @PostMapping("/trigger")
    public Result trigger(@RequestBody Task task) {
        if(StrUtil.isEmpty(task.getId())) {
            return Result.failed("ID不能为空");
        }
        boolean result = taskService.trigger(task);
        return Result.status(result, "");
    }
}
